// pages/appointment/index.js
const app = getApp();
const http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    type: null,
    detail: {},
    dateList: [],
    dateIndex: 0,
    venueList: [],
    userList: [],
    userIndex: -1,
    isCheck: false,

    isUserShow: false,
    cn_name: '',
    id_number: '',
    phone: '',

    is_sign: false,
    invoiceId: null,

    venueIndex: -1,
    cc_name: '',
    isResult: 0,
    resultSucess: 0,

    isContinue: true

  },
  //我的预约详情
  getSignDetail(id) {
    var that = this;
    http.post('api/user/my_sign_detail', {
      id
    }).then(res => {
      var dateList = res.data.segment.map(item => {
        var obj = {
          year: item.date_segment.substring(0, 4),
          date: item.date_segment.substring(5),
          id: item.id,
          sign_num: item.sign_num,
          max_sign_num: item.max_sign_num,

        }
        return obj
      })
      var venueList = res.data.segment.map(item => item.time_segment)
      var dateIndex = 0;
      var venueIndex = 0;

      var userArr = res.data.segment[0].signuser;
      var userList = userArr.map(item => {
        var obj = {
          ev_name: item.ev_name,
          ev_no: item.ev_no,
          ev_tel: item.ev_tel,
          isSelect: 1
        }
        return obj;
      })
      this.setData({
        detail: res.data.detail,
        dateList,
        venueList,
        ev_id: dateList.length > 0 ? dateList[0].id : null,
        userList,
        cc_name: userArr[0] ? userArr[0].name : '',
        dateIndex,
        venueIndex
      })
    })
  },
  getData(id) {
    http.post('api/event/detail_sign', {
      id
    }, 1).then(res => {
      if (res.code == 1) {
        var dateList = res.data.segment.map(item => {
          var obj = {
            year: item.date_segment.substring(0, 4),
            date: item.date_segment.substring(5),
            id: item.id,
            sign_num: item.sign_num,
            max_sign_num: item.max_sign_num,

          }
          return obj
        })
        var venueList = res.data.segment.map(item => item.time_segment)
        this.setData({
          detail: res.data.detail,
          dateList,
          venueList,
          ev_id: dateList.length > 0 ? dateList[0].id : null,
          is_sign: parseInt(res.data.detail.is_sign),
          isContinue: dateList.length > 0 ? this.flagCont(res.data.segment[0]) : 1
        })
      }
    })
  },
  //判断预约人数是否已满
  flagCont(data) {
    return data.max_sign_num == 0 ? true : data.sign_num < data.max_sign_num;
  },
  changeCheck() {
    this.setData({
      isCheck: !this.data.isCheck
    })
  },
  openUser() {
    this.setData({
      isUserShow: true
    })
  },
  closeUser() {
    this.setData({
      isUserShow: false
    })
  },
  changeInput(e) {
    var name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },
  // 选择日期
  selectDate(e) {
    var item = e.currentTarget.dataset.item;
    this.setData({
      dateIndex: e.currentTarget.dataset.index,
      ev_id: item.id,
      isContinue: this.flagCont(item),
      venueIndex: -1,
      cc_name: ''
    })
  },
  // 选择场次
  selectVenue(e) {
    var item = e.currentTarget.dataset.item;
    this.setData({
      venueIndex: e.currentTarget.dataset.index,
      cc_name: item.name,
      // isContinue: this.flagCont(item)
    })
  },
  //选择用户
  selectUser(e) {
    var index = e.currentTarget.dataset.index;
    var userList = this.data.userList.map(item => {
      item.isSelect = !item.isSelect;
      return item;
    });
    this.setData({
      userIndex: index,
      userList
    })
  },
  saveUser() {
    var userData = {
      ev_name: this.data.cn_name,
      ev_no: this.data.id_number,
      ev_tel: this.data.phone,
      isSelect: false
    }
    if (!userData.ev_name || !userData.ev_no || !userData.ev_tel) {
      return wx.showToast({
        title: '信息不能为空！',
        icon: 'none'
      })
    }
    if (!app.validateIdNumber(this.data.id_number)) {
      return wx.showToast({
        title: '请填写正确的身份证号！',
        icon: 'none'
      })
    }
    if (!app.isPhone(this.data.phone)) {
      return wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none'
      })
    }
    var userList = this.data.userList;
    userList.push(userData)
    this.setData({
      userList: userList,
      isUserShow: false
    })
  },
  delUser(e) {
    wx.showModal({
      title: '',
      content: '确认删除此用户？',
      success: (res) => {
        if (res.confirm) {
          var index = e.currentTarget.dataset.index;
          var userList = this.data.userList;
          userList.splice(index, 1)
          this.setData({
            userList
          })
        }
      }
    })

  },
  //去电子签名
  goXz() {
    wx.navigateTo({
      url: '/pages/notice/index'
    })
  },
  //立即预约
  openYuyue() {
    var that = this;
    var invoiceId = that.data.invoiceId;
    if (!that.data.isCheck) {
      return wx.showToast({
        title: '请阅读预约须知并勾选！',
        icon: 'none'
      })
    }
    if (that.data.is_sign && !invoiceId) {
      return wx.showToast({
        title: '请查看预约须知并点击签名！',
        icon: 'none'
      })
    } else {
      var userList = that.data.userList;
      var postData = {
        e_id: that.data.id,
        id: that.data.ev_id,
        t: that.data.cc_name,
        sign_id: invoiceId,
        json: JSON.stringify(userList)
      }
      if (!postData.id || !postData.t || userList.length <= 0) {
        return wx.showToast({
          title: '请填写完整信息！',
          icon: 'none'
        })
      }
      that.setData({
        isResult: 1
      })
      http.post('api/user/ac_sign', postData, 1).then(res => {
        if (res.code == 1) {
          that.setData({
            resultSucess: true
          })
        }else{
			that.setData({
			  resultSucess: false
			})
		}

      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      type: options.type
    })
    if (options.type == 'my') {
      wx.setNavigationBarTitle({
        title: '预约详情'
      })
      return this.getSignDetail(options.id);
    }

    this.getData(options.id)
  },
  goPath(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})